<template>
  <Dialog
    v-model="show"
    :title="props.data?.id ? $t('tagSetting.编辑标签') : $t('tagSetting.新增标签')"
    width="600px"
    @close="close"
    @confirm="handleSubmit"
  >
    <LangTabs v-model="lang" />
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160px">
      <el-form-item :label="$t('tagSetting.标签名称')" :prop="'name' + lang">
        <el-input
          v-model="formData['name' + lang]"
          :placeholder="$t('tagSetting.请输入标签名称')"
        />
      </el-form-item>

      <el-form-item :label="$t('tagSetting.排序')" prop="sort">
        <el-input-number
          v-model="formData.sort"
          :placeholder="$t('tagSetting.请输入')"
          style="width: 100%"
        />
      </el-form-item>

      <!-- <el-form-item label="备注" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          :rows="4"
          placeholder="请输入备注信息"
        />
      </el-form-item> -->
    </el-form>
  </Dialog>
</template>

<script setup>
import { ref, watch, reactive } from "vue";
import Dialog from "@/components/dialog.vue";
import LangTabs from "@/components/LangTabs.vue";
import { useI18n } from "vue-i18n";
import i18n from "@/i18n";
import { ElMessage } from "element-plus";
import Api from "@/config/api/api";

const { locale, t } = useI18n();

const lang = ref(locale.value);
const langs = reactive(Object.keys(i18n.global.messages.value));
const show = defineModel();
const emit = defineEmits(["success"]);

const props = defineProps({
  data: {
    type: Object,
    default: () => ({}),
  },
});

// 表单数据
const formData = ref({
  sort: 0,
});
// 表单校验规则
const rules = {
  sort: [{ required: true, message: t('tagSetting.请输入排序'), trigger: "blur" }],
};

langs.forEach((item) => {
  formData.value["name" + item] = "";
  rules["name" + item] = [
    { required: true, message: t('tagSetting.请输入标签名称'), trigger: "blur" },
    // { max: 20, message: "标签名称不能超过20个字符", trigger: "blur" },
  ];
});

// 当弹窗打开且有data数据时，加载现有数据进表单
watch(show, (val) => {
  if (val && props.data?.id) {
    // 编辑模式，加载数据
    formData.value = { ...props.data };
  }
});

const formRef = ref();

const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      const submitData = {
        ...formData.value,
        // name: formData.value.nameZhCN,
        // sort: 0,
        // 如果是编辑模式，保留ID
        ...(props.data?.id ? { id: props.data.id } : {}),
      };
      const port = props.data?.id
        ? Api.skuAttribute_edit
        : Api.skuAttribute_add;
      port(submitData).then((res) => {
        if (res.status == 200) {
          ElMessage.success(t('tagSetting.操作成功'));
          emit("success", submitData);
          close();
        }
      });
    }
  });
};

const close = () => {
  // 重置表单数据
  formData.value = {
    sort: 0,
  };
  langs.forEach((item) => {
    formData.value["name" + item] = "";
  });
  show.value = false;
};
</script>

<style lang="scss" scoped>
// 可以添加特定的样式
</style>
